package com.menggudf.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.menggudf.convert.PartsCoordinateConvert;
import com.menggudf.dao.PartsCoordinateDao;
import com.menggudf.entity.PartsCoordinate;
import com.menggudf.model.PartsCoordinateModel;
import com.menggudf.model.ResultModel;
import com.menggudf.service.PartsCoordinateService;

@Service
public class PartsCoordinateServiceImpl implements PartsCoordinateService{
    
    private static final Logger LOGGER = LoggerFactory
            .getLogger(PartsCoordinateServiceImpl.class);

    @Autowired
    private PartsCoordinateDao partsCoordinateDao;
    
    @Autowired
    PartsCoordinateConvert partsCoordinateConvert;
    
    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel saveCoordinate(
            String part,
            String sexflag,
            String prosAndCons,
            String coordinate_X,
            String coordinate_Y) {
        PartsCoordinate  partsCoordinate = partsCoordinateDao.findByPart(part);
/*        if(partsCoordinate!=null){
            return ResultModel.fail();
        }*/
        PartsCoordinate pCoordinate = new PartsCoordinate();
        //pCoordinate.setAuthor(author);
        pCoordinate.setCoordinate_X(coordinate_X);
        pCoordinate.setCoordinate_Y(coordinate_Y);
        pCoordinate.setCreateTime(new Date());
        pCoordinate.setParts(part);
        pCoordinate.setSexflag(sexflag);
        pCoordinate.setProsAndCons(prosAndCons);
        try {
            partsCoordinateDao.persist(pCoordinate);
            return ResultModel.success("保存成功", null);
        } catch (Exception e) {
            LOGGER.error("saveCoordinate fail",e);
        }
        return ResultModel.fail("保存失败",null);
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public List<PartsCoordinateModel> getAllCoordinate(String sexflag, String prosAndCons) {
        List<PartsCoordinate> list = partsCoordinateDao.getAllCoordinate(sexflag,prosAndCons);
        List<PartsCoordinateModel> mlist = new ArrayList<PartsCoordinateModel>(); 
        for(PartsCoordinate p : list){
            PartsCoordinateModel model = partsCoordinateConvert.entity2PartsCoordinateModelConvert(p);
            mlist.add(model);
        }
        return mlist;
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel deletePoint(String coordinate_X, String coordinate_Y,String sexflag, String prosAndCons) {
        PartsCoordinate  partsCoordinate = partsCoordinateDao.findByCoordinate(coordinate_X,coordinate_Y, sexflag, prosAndCons);
        try {
            partsCoordinateDao.remove(partsCoordinate);
            return ResultModel.success();
        } catch (Exception e) {
            LOGGER.error("deletePoint fail",e);
        }
        return ResultModel.fail();
    }

}
